﻿using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace XUBugly
{
    namespace Editor
    {
        [InitializeOnLoad]
        internal class BuglyInitTempFolder
        {
            static BuglyInitTempFolder()
            {
                if (!Directory.Exists(tempFolder))
                {
                    Directory.CreateDirectory(tempFolder);
                }

                string jarFromPath = "buglyqq-upload-symbol.jar";
                {
                    List<string> allFiles = new List<string>();
                    string folder1 = Application.dataPath.Replace("Assets", "Packages");
                    string folder2 = Application.dataPath.Replace("Assets", "Library/PackageCache");
                    allFiles.AddRange(Directory.GetFiles(folder1, jarFromPath, SearchOption.AllDirectories));
                    allFiles.AddRange(Directory.GetFiles(folder2, jarFromPath, SearchOption.AllDirectories));
                    foreach (string file in allFiles)
                    {
                        jarFromPath = file;
                        break;
                    }
                }

                string jarToPath = tempFolder + "/" + Path.GetFileName(jarFromPath);
                File.Copy(jarFromPath, jarToPath, true);
            }

            public static string tempFolder
            {
                get { return Application.dataPath.Replace("Assets", "Library/XUBugly"); }
            }
        }
    }
}